package com.cac.demo.service.FinProReceive;

import com.alibaba.fastjson.JSONObject;
import com.cac.demo.dao.finProReceive.FinProReceiveSearchDao;
import com.cac.demo.dao.partReceive.PartReceiveDao;
import com.cac.demo.entity.materialScan.WmsDeliverPlanDTO;
import com.cac.demo.service.gkinterface.WorkService;
import com.cac.demo.util.DateUtil;


import com.cac.demo.util.adapter.DaoException;
import com.cac.demo.util.adapter.QueryReqBean;
import com.cac.demo.util.adapter.QueryRespBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class FinProReceiveSearchService {

    @Autowired
    private FinProReceiveSearchDao finProReceiveSearchDao;
    @Autowired
    private PartReceiveDao partReceiveDao;
    @Autowired
    private Environment environment;
    @Autowired
    private WorkService workService;

    //成品接收页面条件查询
    public QueryRespBean<WmsDeliverPlanDTO> searchParamsByItemFS(QueryReqBean<WmsDeliverPlanDTO> queryReqBean, String orderBy, String Keywords, Date startTime, Date endTime,
                                                                 String itemCode) {
        QueryRespBean<WmsDeliverPlanDTO> queryRespBean = new QueryRespBean<>();
        try {
            PageHelper.startPage(queryReqBean.getPageParameter());
            WmsDeliverPlanDTO searchParams = queryReqBean.getSearchParams();
            Page<WmsDeliverPlanDTO> dataList = finProReceiveSearchDao.searchParamsByItemFS(searchParams, orderBy, Keywords, startTime, endTime, itemCode);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    //条码查询
    public QueryRespBean<List<HashMap>> searchParamsByBarcodeFS(QueryReqBean<HashMap> queryReqBean, String orderBy, String Keywords,
                                                                String barcode, String shop) {
        QueryRespBean<List<HashMap>> queryRespBean = new QueryRespBean<>();
        try {
            PageHelper.startPage(queryReqBean.getPageParameter());
            HashMap searchParams = queryReqBean.getSearchParams();
            if(searchParams==null){
                searchParams = new HashMap();
                searchParams.put("requireShop",shop);
            }else{
                searchParams.put("requireShop",shop);
            }
            Page<List<HashMap>> dataList = finProReceiveSearchDao.searchByBarcodeFS(searchParams, orderBy, Keywords, barcode);
            log.info("dalist:" + dataList);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        } catch (Exception e) {
            e.printStackTrace();
            throw new DaoException(e.getMessage(), e);
        }
    }

    //判断结果集
    public List<HashMap> boolResult(String barcode) {
        List<HashMap> dataList = finProReceiveSearchDao.searchByBarcodeFs(barcode);
        return dataList;
    }

    //判断成品合格证是否重复
    public  boolean  queryCpHgzIsRepeat(String barcode,String shop){
        //queryCpHgzIsRepeat
        int cnt = finProReceiveSearchDao.queryCpHgzIsRepeat(barcode,shop);
        if(cnt>1){
            return true;
        }
        return false;
    }
    //根据前端按钮修改接收物料状态 cp
    @Transactional
    public String changeMaterialSate(String barcode, String receiveEmpCode, String IP,String wmsDeliveryPlanId)  {
        String result = null;
        try {
            //接收归零
//            partReceiveDao.updateCoByBarcode(barcode, receiveEmpCode);
            //修改交接单状态
            finProReceiveSearchDao.changeTransferType(barcode, DateUtil.nowTime(), receiveEmpCode, IP,wmsDeliveryPlanId);
            result = "OK";
        } catch (Exception e) {
            log.info("异常信息抛出！");
            result = "IllegalBarcode";
            e.printStackTrace();
            throw new DaoException(e.getMessage());
        } finally {
            return result;
        }
    }

    //批量接收
    public String changeMaterialSates(List<String> barcodes, String receiveEmpCode, String IP) throws ParseException {
        String result = "";
     /*   for (int i = 0; i < barcodes.size(); i++) {
            Integer exist = finProReceiveSearchDao.searchBarCode(barcodes.get(i));
            try {
                //条码是否存在
                if (exist != null) {
                    int receiveQty = finProReceiveSearchDao.searchObject(barcodes.get(i));
                    finProReceiveSearchDao.changeTransferType(barcodes.get(i), DateUtil.nowTime(), receiveEmpCode, IP);
                    result = "OK";
                } else {
                    result = "IllegalBarcode";
                }
            } catch (Exception e) {
                throw new DaoException(e.getMessage());
            }
        }*/
        return result;
    }

    //查询是否发起协调
    public HashMap searchCoordinate(String barcode) {
        return finProReceiveSearchDao.searchCoordinate(barcode);
    }


    //归零接收
    @Transactional
    public String coordinateReceive(String barcode, String WMS_DELIVERY_COORDINATE_ID) {
        HashMap map = finProReceiveSearchDao.coordinateReceive(barcode);
        map.put("wmsCoordinateInfoId", WMS_DELIVERY_COORDINATE_ID);
        map.put("locatorNo", environment.getProperty("doReturnErp.scanZeroReceiveCp.locatorNo"));
        JSONObject data = new JSONObject(map);
        //放入请求头
        JSONObject headersMap = new JSONObject();
        headersMap.put("func", environment.getProperty("doReturnErp.scanZeroReceiveCp.func"));
        headersMap.put("data", data);
        String headersmap = headersMap.toJSONString();
        log.info("===========================工控网上传_成品归零接收===========================");
        //工控网上传数据
        HashMap issuedTest = new HashMap();
        issuedTest.put("BData", headersmap);
        issuedTest.put("type", environment.getProperty("doReturnErp.scanZeroReceiveCp.func"));
        log.info("工控网上传程序后台日志:>>>>>>>>>>>>>>>>>>>>>>>>>issuedParameters:" + issuedTest.toString());
        log.info("===========================工控网上传_成品归零接收===========================");
        workService.issuedTest(issuedTest);
        //对已经转换成字符串的数据进行加密
        final Base64.Encoder encoder = Base64.getEncoder();
        String BData = encoder.encodeToString(headersmap.getBytes());
        return BData;
    }

}
